fix(readonly): avoid changing permissions when not needed
authorMatthieu Gallien <matthieu.gallien@nextcloud.com>
Fri, 30 May 2025 15:53:04 +0000 (17:53 +0200)
committerMatthieu Gallien <matthieu.gallien@nextcloud.com>
Mon, 2 Jun 2025 11:14:15 +0000 (13:14 +0200)
in FilePermissionsRestore, first checks the folder permissions before
changing them

will avoid useless call changing permissions when not needed

Signed-off-by: Matthieu Gallien <matthieu.gallien@nextcloud.com>
src/libsync/filesystem.cpp
test/testpermissions.cpp

index 515d02b1581c85516c4698a8b2fa1f6ddfa847d4..5603e4f4eaa384769155915faf3350d86ceee572 100644 (file)
@@ -648,12 +648,13 @@ FileSystem::FilePermissionsRestore::FilePermissionsRestore(const QString &path,
 {
     try
     {
-        const auto stdStrPath = _path.toStdWString();
-        _initialPermissions = FileSystem::isFolderReadOnly(stdStrPath) ? OCC::FileSystem::FolderPermissions::ReadOnly : OCC::FileSystem::FolderPermissions::ReadWrite;
-        if (_initialPermissions != temporaryPermissions) {
+        const auto &stdStrPath = _path.toStdWString();
+        const auto fsPath = std::filesystem::path{stdStrPath};
+        if ((temporaryPermissions == OCC::FileSystem::FolderPermissions::ReadOnly && !FileSystem::isFolderReadOnly(fsPath)) ||
+            (temporaryPermissions == OCC::FileSystem::FolderPermissions::ReadWrite && FileSystem::isFolderReadOnly(fsPath))) {
+            FileSystem::setFolderPermissions(_path, temporaryPermissions);
             _rollbackNeeded = true;
         }
-        FileSystem::setFolderPermissions(_path, temporaryPermissions);
     }
     catch (const std::filesystem::filesystem_error &e)
     {
index f04f14b003f9c3feaf505b6f4c52cdc147215e3e..5de4c4dd636750c3712aa088f3123f5f9d7b4854 100644 (file)
@@ -278,11 +278,7 @@ private slots:
 
         //4. File should be updated, that's tested by assertLocalAndRemoteDir
         QCOMPARE(currentLocalState.find("normalDirectory_PERM_CKDNV_/canBeModified_PERM_W_.data")->size, canBeModifiedSize + 1);
-#if defined Q_OS_WINDOWS
-        QCOMPARE(currentLocalState.find("readonlyDirectory_PERM_M_/canBeModified_PERM_W_.data")->size, canBeModifiedSize);
-#else
         QCOMPARE(currentLocalState.find("readonlyDirectory_PERM_M_/canBeModified_PERM_W_.data")->size, canBeModifiedSize + 1);
-#endif
 
         //5.
         // the file should be in the server and local